package com.wholesmart.common.security.listener;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationListener;
import org.springframework.security.authentication.event.AuthenticationFailureBadCredentialsEvent;
import org.springframework.stereotype.Component;

/**
 * 鉴权失败时间监听器
 * 
 * @author dyw
 * @date 2019年11月26日
 */
@Component
public class AuthenticationFailureListener implements ApplicationListener<AuthenticationFailureBadCredentialsEvent> {
	@Autowired
	private LoginAttemptService loginAttemptService;

	@Override
	public void onApplicationEvent(AuthenticationFailureBadCredentialsEvent e) {
		Object principal = e.getAuthentication().getPrincipal();
		if (principal instanceof String) {
			String username =  principal.toString();
			loginAttemptService.loginFailed(username);
		}
	}
}
